package wfb.frontend.threads;

import wfb.definitions.CountryDataInterface;
import wfb.definitions.WfbDbInterface;
import wfb.definitions.threadInterfaces.FillEditorPaneInterface;

import javax.swing.*;
import java.awt.*;

/**
 * @author Tobias Peitzsch
 */
public class FillEditorPaneThread implements FillEditorPaneInterface {
	private JEditorPane pane=null;
	private int mode=-1;
	private WfbDbInterface wdi=null;
	private String country=null;
	/* (non-Javadoc)
	 * @see wfb.definitions.FillEditorPaneInterface#setJEditorPane(javax.swing.JEditorPane)
	 */
	public void setJEditorPane(JEditorPane pane) {
		this.pane=pane;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.FillEditorPaneInterface#setMode(int)
	 */
	public void setMode(int mode) {
		this.mode=mode;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.FillEditorPaneInterface#setWfbDbImpl(wfb.definitions.WfbDbInterface)
	 */
	public void setWfbDbImpl(WfbDbInterface wdi) {
		this.wdi=wdi;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.FillEditorPaneInterface#setCountry(java.lang.String)
	 */
	public void setCountry(String country) {
		this.country=country;
	}
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		if(pane==null||mode<0||country==null||wdi==null)return;
		if(mode == FillEditorPaneInterface.LONG_COUNTRY_INFO){
			final String text = fillLongCountryInfo(wdi.getCountryData(country));	
			EventQueue.invokeLater(new Runnable(){
				public void run(){
					try {
						pane.setText(text);
						pane.setCaretPosition(1);
					}
					catch (Exception e) {
						pane.setText("Fehler: " + e);
					}
				}
			});
		}
		else if(mode==FillEditorPaneInterface.SHORT_COUNTRY_INFO){
				final String text = fillShortCountryInfo(wdi.getCountryData(country));
				EventQueue.invokeLater(new Runnable(){
					public void run(){
						try {
							pane.setText(text);
						}
						catch (Exception e) {
							pane.setText("Fehler: " + e);
						}
					}
				});
			}
		}
	private String fillLongCountryInfo(CountryDataInterface myCountry){
		if(myCountry==null){
			return "keine Informationen\n \n ";
		}
		else{ 
			return myCountry.getTextInfo();
		}
	}
	private String fillShortCountryInfo(CountryDataInterface myCountry){
		if(myCountry==null){
			return "keine Informationen";
		}
		else{ 
			return "<B>Langform Laendername :</B> " + myCountry.getLongName() +
													 "<P><B>Hauptstadt :</B> " + myCountry.getCapital().getName() +
													 "<P><B>Laenge und Breite :</B> " + myCountry.getLongitude() +
													 " long && " + myCountry.getLatitude() + " lat" +
													 "<P><B>Lage :</B> " + myCountry.getTextLage();
		}		
	}
}